/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.great.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.beans.User;
import br.great.exception.*;

/**
 *
 * @author C�line DEFAUT
 */
public class DAO_User {
    public static volatile DAO_User instanceDAOUser = new DAO_User();
    
    /** The private constructor of DAO_User for singleton
    */
    private DAO_User(){
    
    }
    
    /** The method for get the instance of the class DAO_User
    * @return DAO_User instaceDAO_User
    */
    public static DAO_User getInstance() {
        //if(instaceDAO_User==null)
           synchronized(DAO_User.class) {
             if (instanceDAOUser == null)
               instanceDAOUser = new DAO_User();
           }
        return instanceDAOUser;
    }
    
    /** The method getUserByLoginAndPassword(String login, String password)
     * get the information of the user who has the login and the password.
    * @param String login, String password
    * @exception SQLException ex
    * @return User bu
    */
    public User getUserByLoginAndPassword(String login, String password) throws UnknownUserException {
       /* PgConnect conn = PgConnect.getInstance();
        try {
            String getUser = "select * from users u ,foaf_person f where login = '"
                    + login + "' and password_2 = '" + password + "' and f.FOAF_PROFIL=u.FOAF_PROFIL;";
            ResultSet rs = conn.executeQuery(getUser);
            if (rs.next()) {
                User bu = new User();
                bu.setLogin(login);
                bu.setPassword(password);
                bu.setName(rs.getString("name"));
                bu.setFirstName(rs.getString("firstname"));
                bu.setURLPhoto(rs.getString("photo"));
                bu.setURLFOAFProfil(rs.getString("foaf_profil"));
                conn.Close();
                return bu;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_User.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnknownUserException();
        } 
        conn.Close();*/
        return new User();
    }

    public void updateNumberNewCollection(User user) {
        PgConnect conn = PgConnect.getInstance();
        String updateNewCollection = "UPDATE users SET nb_new_collection=(SELECT nb_new_collection FROM users WHERE login ='"+user.getLogin()+"')+1 WHERE login ='"+user.getLogin()+"'";
        conn.executeUpdate(updateNewCollection);
        conn.Close();
    }
    
//    public static void main(String[] args){
//        DAO_User  d = new DAO_User();
//        User u = d.getUserByLoginAndPassword("celine", "asdfgh");
//    }
}
